﻿using CSharpDay15__Auth_.Adapters.Interfaces;
using CSharpDay15__Auth_.Data;
using CSharpDay15__Auth_.Data.Model;
using CSharpDay15__Auth_.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CSharpDay15__Auth_.Adapters.Adapters
{
    public class ToDoAdapter : IToDoAdapter
    {
        //find all todo's with a specific user id
        public List<ToDo> GetToDos(string userId)
        {
            List<ToDo> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.ToDoes.Where(x => x.UserId == userId).ToList();
            }
            return model;
        }

        public List<UserViewModel> GetAllUsers()
        {
            List<UserViewModel> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Users.Select(x => new UserViewModel()
                {
                    Id = x.Id,
                    IsDisabled = x.IsDisabled,
                    NumToDos = x.ToDos.Count,
                    UserName = x.UserName
                }).ToList();
            }
            return model;
        }

        public void ToggleIsDisabled(string userId)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                ApplicationUser user = db.Users.FirstOrDefault(x => x.Id == userId);
                user.IsDisabled = !user.IsDisabled;
                db.SaveChanges();
            }
        }
    }
}